!!ARBfp1.0
# Simple program to show how to code up the default texture environment

#I/O with the rest of OGL
ATTRIB pixel_obj = fragment.texcoord[0];
ATTRIB water_samp = fragment.texcoord[1];

PARAM N = program.local[0];					# Water normal (normalized) - precomputed and const
PARAM near_plane_info = program.local[1];	# A few more precomputed things: xy scale eye coords to -1, 1, z = far clip plane

PARAM M1 = program.local[2];
PARAM M2 = program.local[3];
PARAM M3 = program.local[4];
PARAM M4 = program.local[5];

PARAM back_plane = program.local[6];

PARAM camera_loc = program.local[7];

OUTPUT outColor = result.color;
OUTPUT depth = result.depth;

#Local Variables
TEMP V;			# Vector from camera to water
TEMP s;			# a temp
TEMP R;			# Reflection vector
TEMP texel;		# TExel we will fetch
TEMP disp;		# displacement to make shimmer

#Constants
PARAM zero = { 0.0, 0.0, 0.0, 0.0 };
PARAM two = { 2.0, 2.0, 2.0, 2.0 };
PARAM half = { 0.5, 0.5, 0.5, 0.5 };
PARAM one = { 1.0, 1.0, 1.0, 1.0 };
PARAM amp = { 10.1, 40.1, 1.1, 1.1 };

###############################################################################################################
# SHITTY SHIMMER EFFECT
###############################################################################################################
# Just grab an alpha level from the water, sin-wave it, and amp it.

TEX disp, water_samp, texture[1], 2D;
MOV disp, -disp.w;
MUL disp, disp, amp;

###############################################################################################################
# Find "V" - this is the vector from the water droplet to the camera.
###############################################################################################################
SUB V, camera_loc, pixel_obj;

# Find "R" - this is the vector from the water droplet to, well, whatever we see in it, e.g. the mountain.
# We use R = V-2N(NdotV).  Basically scale the normal to the height of the incoming vector, double it, 
# and you have pieces to get outgoing vector

DP3 s, N, V;
MUL s, two, s;
MUL s, N, s;
MAD s, N, s, disp;
SUB R, s, V;

# Debug code - we could use this to visualize the various calcs above

#MOV R, V;

#DP3 s, R, R;
#RSQ s.w, s.w;
#MUL s, s.w, R;
#MAD outColor, s, half, half;


#Don't remmber what th is was for
#MOV outColor.x, s;
#MOV outColor.y, -s.x;
#MOV outColor, R;
#MOV outColor.z, zero;

###############################################################################################################
# Calculate "S" (s.z) - the scaling ratio to put R on the far clip plane.
###############################################################################################################

# pixel_obj is the location of our water droplet (mv coords).
# R is a vector toward the back-plane in (mv coord's) from that droplet.
# back_plane is the noram vector (xyz) and opp-side constant (w).

# Formula for the intersection is actually
# ndotp - n dot P
#  ------------
#    n dot V
#
# Where: P = point on line, V = vector on line, N = plane normal, ndotp = plane normal dot pt on plane

# Here s.x will be the numerator expression, s.y will be the denomniator

DP3 s.y, back_plane, R;						# n dot V
DP3 s.x, back_plane, pixel_obj;				# Find n dot P
SUB s.x, back_plane.w, s.x;					# ndotp - n dot P
RCP s.y, s.y;								# 1/  n dot V
MUL s, s.x, s.y;							# s = scalar is the right equation now
MAD texel, R, s, pixel_obj;					# result = R * s + P


# Now find the point where our reflection hits the far plane from S.

MAD texel, R, s, pixel_obj;
MOV texel.w, one;							# Possibly 'w' param is f-cked by vectors.  Fix it!

#MOV texel, pixel_obj;

###############################################################################################################
# TURN POINT TO A TEXEL VIA CAMERA
###############################################################################################################
#alternate hack to get texel coords - just use projection matrix
#MOV texel, pixel_obj;
DP4 s.x, M1, texel;
DP4 s.y, M2, texel;
DP4 s.z, M3, texel;
DP4 s.w, M4, texel;

#Ben sez: note we do the /w before the *0.5+0.5 rescale
#we'd have to be careful not to rescale the W coord if we wanted
#to use projective-texturing op rather than this.
RCP s.w, s.w;
MUL texel, s, s.w;
MAD texel, texel, half, half;

# Take our texel on the near clip plane and transform it to 
# texture coords by dividing by width and height and adding 0.5

#MUL texel, texel, near_plane_info;
#MAD texel, texel, half, half;

# clamp tex coords
#MIN texel, one, texel;
#MAX texel, zero, texel;

# Finally sample the texture

TEX outColor, texel, texture[0], 2D;           #sample the texture


END
